﻿using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.CRM.BigCustomerDriver;
using BCDto.CRM.BigCustomerDriver;
using BCEntity.CRM.BigCustomerDriver;
using BCDto.Sim.Admin.Admin;
using BCData.CRM.BigCustomer;
using BCData.TMS.Driver;
using BCData.CRM.CustomerInfo;
using BCEntity.TMS.Driver;
using System.Linq;
using BCEntity.Common.EntityNameConst;
using BCService.MapperProfiles.TMS.SpecificServiceType;
using BCDto.TMS.SpecificServiceType;

namespace BCService.TMS.BigCustomerDriver
{
    public class BigCustomerDriverService : IBigCustomerDriverService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private IBigCustomerDriverData bigCustomerDriverData;
        private IDriverData driverData;
        private ICustomerInfoData customerInfoData;
        private IBigCustomerData bigCustomerData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.BigCustomerDriverEntity);
        #endregion

        #region 构造函数
        public BigCustomerDriverService(
             IBigCustomerData bigCustomerData,
            ICustomerInfoData customerInfoData,
            IDriverData driverData,
            IBigCustomerDriverData bigCustomerDriverData,
            IDatabaseContext databaseContext,
            IRedisService redisService)
        {
            this.bigCustomerData = bigCustomerData;
            this.customerInfoData = customerInfoData;
            this.driverData = driverData;
            this.bigCustomerDriverData = bigCustomerDriverData;
            this.databaseContext = databaseContext;
            this.redisService = redisService;
        }
        #endregion

        #region Add
        /// <summary>
        /// 添加新大客户专属司机纪录
        /// </summary>
        /// <param name="bigCustomerDriverRequestDto"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public BigCustomerDriverDto Add(BigCustomerDriverRequestDto bigCustomerDriverRequestDto, AdminDto admin)
        {
            DriverEntity dirverEntity = driverData.GetDriverByIdAsync(bigCustomerDriverRequestDto.DriverId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到司机信息!");
            var customerViewEntity = bigCustomerData.GetBigCustomerViewEntityAsync(bigCustomerDriverRequestDto.BigCustomerId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到客户信息");
            var list = bigCustomerDriverData.GetOnlyList(null, bigCustomerDriverRequestDto.BigCustomerId, bigCustomerDriverRequestDto.DriverId).GetAwaiter().GetResult();
            if (list != null && list.Count() > 0)
                throw new ArgumentException("要添加的记录有重复!");

            BigCustomerDriverEntity bigCustomerDriverEntity = bigCustomerDriverRequestDto.As<BigCustomerDriverEntity>();

            bigCustomerDriverEntity.CompanyId = customerViewEntity.CompanyId;
            bigCustomerDriverEntity.CustomerInfoId = customerViewEntity.CustomerInfoId.Value;
            bigCustomerDriverEntity.BigCustomerId = customerViewEntity.BigCustomerId;
            bigCustomerDriverEntity.CustomerUserName = customerViewEntity.UserName;

            bigCustomerDriverEntity.DriverId = dirverEntity.DriverId;
            bigCustomerDriverEntity.DriverName = dirverEntity.Name;
            bigCustomerDriverEntity.DriverPhone = dirverEntity.Phone;

            bigCustomerDriverEntity.VehicleTypeId =   dirverEntity.VehicleTypeId;
            bigCustomerDriverEntity.VehicleType = dirverEntity.VehicleType;

            bigCustomerDriverEntity.CreateAdminId = admin.AdminId;
            bigCustomerDriverEntity.CreateAdminName = admin.FullName;
            bigCustomerDriverEntity.CreateTime = DateTime.Now;
            return bigCustomerDriverData.InsertAsync(bigCustomerDriverEntity).GetAwaiter().GetResult().As<BigCustomerDriverDto>();
        }

        public void AddList(List<BigCustomerDriverRequestDto> bigCustomerDriverRequestDtoList, AdminDto admin)
        {
            foreach (var item in bigCustomerDriverRequestDtoList)
            {
                Add(item, admin);
            }
        }
        #endregion

        #region update 
        /// <summary>
        /// 修改大客户司机车型
        /// </summary>
        /// <param name="vehicleCategoryId">车型Id</param>
        /// <param name="bigCustomerDriverId">大客户专属司机Id</param>
        /// <param name="admin">管理员</param>
        public BigCustomerDriverDto UpdateVehicleCategory(int vehicleCategoryId, long bigCustomerDriverId, AdminDto admin)
        {
            throw new Exception("此功能暂时不支持");
            //var entity = bigCustomerDriverData.GetEntityAsync(bigCustomerDriverId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到要修改的记录!");

            
            //return bigCustomerDriverData.UpdateAsync(bigCustomerDriverDto.As<BigCustomerDriverEntity>()).GetAwaiter().GetResult().As<BigCustomerDriverDto>();

        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除大客户专属司机记录
        /// </summary>
        /// <param name="bigCustomerDriverId"></param>
        /// <returns></returns>
        public void Delete(long bigCustomerDriverId)
        {
            this.redisService.DeleteMultipleKey(key, false);
            bigCustomerDriverData.DeleteAsync(bigCustomerDriverId).GetAwaiter().GetResult();

        }
        #endregion

        #region GetByBigCustomerDriverId
        /// <summary>
        /// 获取大客户专属司机记录
        /// </summary>
        /// <param name="bigCustomerDriverId"></param>
        /// <returns></returns>
        public BigCustomerDriverDto GetById(long bigCustomerDriverId)
        {
            string cacheKey = this.redisService.GetKey(key, bigCustomerDriverId);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = bigCustomerDriverData.GetEntityAsync(bigCustomerDriverId).GetAwaiter().GetResult();
                return a.As<BigCustomerDriverDto>();
            }
            , TimeSpan.FromDays(1));
            return result.As<BigCustomerDriverDto>();
        }
        #endregion

        #region GetList  获取大客户专属司机记录--[分页]
        public PageableList<BigCustomerDriverDto> GetList(AdminDto admin, long? bigCustomerId, string driverName, string driverPhone, int pageIndex, int pageSize)
        {
            int fCount = 0;
            IEnumerable<BigCustomerDriverEntity> fList;

            string cacheKey = this.redisService.GetKey(key, admin.CompanyId, bigCustomerId, driverName, driverPhone, pageIndex, pageSize);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                (fList, fCount) = bigCustomerDriverData.GetListAsync(admin.CompanyId, bigCustomerId, driverName, driverPhone, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<BigCustomerDriverDto>()
                {
                    Count = fCount,
                    Items = fList.As<IEnumerable<BigCustomerDriverDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region GetOnlyList
        public List<BigCustomerDriverDto> GetOnlyList(AdminDto admin, long? bigCustomerId)
        {
            return bigCustomerDriverData.GetOnlyList(admin.CompanyId, bigCustomerId, null).GetAwaiter().GetResult().As<List<BigCustomerDriverDto>>();
        }
        #endregion
    }
}